<template>
  <div class="orderList">
    <div class="title">我的订单</div>
    <el-table :data="tableDate.list" border style="width: 100%">
      <el-table-column prop="id" label="订单编号" width="200"></el-table-column>
      <el-table-column prop="status" label="订单状态" width="150">
        <template #default="scope">
          <el-tag :type="getOrderStatusType(scope.row.status)">
            {{ getOrderStatusText(scope.row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="200"></el-table-column>
      <el-table-column label="操作" width="150">
        <template #default="scope">
          <el-button v-if="scope.row.status === 0" @click="goToPay(scope.row.id)" type="danger" size="small"
                     style="width: 70px">去支付
          </el-button>
          <el-button v-else @click="viewOrderDetail(scope.row)" type="primary" size="small" style="width: 70px">
            查看详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <div>
    <el-row class="row-bg" justify="space-evenly">
      <el-col :span="6">
        <el-pagination layout="prev, pager, next" :total="tableDate.total" :page-size="tableDate.pageSize"
                       @current-change="getTableDate"/>
      </el-col>
    </el-row>
  </div>

  <!-- 订单详情弹窗 -->
  <el-dialog v-model="orderDetailVisible" width="50%" title="订单详情">
    <div v-if="selectedOrder">
      <div class="orderInfo">
        <p>订单编号：{{ selectedOrder.id }}</p>
        <p>创建时间：{{ selectedOrder.createTime }}</p>
        <p>订单状态：{{ getOrderStatusText(selectedOrder.status) }}</p>
        <p>总金额：￥{{ selectedOrder.totalAmount }}</p>
      </div>
      <el-divider></el-divider>
      <div class="goodsList">
        <p class="title">订单商品</p>
        <ul>
          <li v-for="(detail,index) in selectedOrder.orderDetails" :key="index">
            <el-row>
              <el-col :span="2">
                <el-image
                    :src="'/api/file/images/' + detail.goods.picList[0].url"
                    style="height: 50px;width: 50px;border-radius: 5px;"
                    fit="contain"
                />
              </el-col>
              <el-col :span="6">{{ detail.goods.name }}</el-col>
              <el-col :span="4">{{ detail.goods.color }} - {{ detail.goods.version }}</el-col>
              <el-col :span="3">￥{{ detail.goods.price }}</el-col>
              <el-col :span="3">数量：x{{ detail.goods.count }}</el-col>
              <el-col :span="3">总价：￥{{ detail.goods.price * detail.count }}</el-col>
            </el-row>
          </li>
        </ul>
      </div>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="orderDetailVisible = false">取消</el-button>
        <el-button v-if="selectedOrder.status === 2" type="primary" @click="updateStatus(selectedOrder.id,3)">确认收货</el-button>
        <el-button v-if="selectedOrder.status > 2" type="primary" @click="goToFeedback(selectedOrder.id)">我要反馈</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import {ref} from 'vue'
import orderApi from '@/api/OrderApi.js'
import {ElMessage} from 'element-plus'
import router from "@/router/index.js";

const orderList = ref([])
const tableDate = ref({
  pageNum: 1,
  pageSize: 5,
  total: 1,
  list: []
})
const selectedOrder = ref(null)
const orderDetailVisible = ref(false)

const condition = ref({
  id: null,
  status: null
})

// 获取订单状态文本  0-未支付，1-已支付，2-已发货，3-已收货，4-退货退款，5-退换货，6-仅退款，7-售后，8-其它
const getOrderStatusText = (status) => {
  switch (status) {
    case 0:
      return '未支付'
    case 1:
      return '已支付'
    case 2:
      return '已发货'
    case 3:
      return '已收货'
    case 4:
      return '退货退款'
    case 5:
      return '退换货'
    case 6:
      return '仅退款'
    case 7:
      return '售后'
    case 8:
      return '其它'
    default:
      return '未知状态'
  }
}

// 获取订单状态标签类型
const getOrderStatusType = (status) => {
  switch (status) {
    case 1:
      return 'danger'
    case 2:
      return 'success'
    case 3:
      return 'warning'
    case 4:
      return 'info'
    case 5:
      return 'danger'
    default:
      return 'default'
  }
}

// 查看订单详情
const viewOrderDetail = (order) => {
  selectedOrder.value = order
  orderDetailVisible.value = true
}

// 获取订单列表
const getTableDate = (pageNum) => {
  orderApi.getList(condition.value, pageNum, 5)
      .then(resp => {
        if (resp.code === 10000) {
          tableDate.value = resp.data
        } else {
          ElMessage.error(resp.msg)
        }
      })
}

// 去支付
const goToPay = (id) => {
  router.push({
    path: '/user/pay',
    query: {
      orderId: id
    }
  })
}

// 修改状态方法
const updateStatus = (id,status) => {
  let order = {
    id:id,
    status:status
  }
  // 修改状态方法
  orderApi.updateInfo(order)
      .then(resp => {
        if (resp.code === 10000) {
          ElMessage.success(resp.msg)
        }else {
          ElMessage.error(resp.msg)
        }

      //   刷新表格
        getTableDate(tableDate.value.pageNum)
        orderDetailVisible.value = false
      })
}

// 去更多页面
const goToFeedback = (id) => {
  router.push({
    path: '/user/center/feedback',
    query: {
      orderId: id,
      payNum:tableDate.value.pageNum
    }
  })
}

getTableDate(1)
</script>

<style scoped>
.orderList {
  margin: 20px 0;
  padding: 20px;
  border: 2px solid #222;
  background-color: #FFFFFF;
}

.title {
  font-weight: bold;
  font-size: 18px;
  margin-bottom: 20px;
}

.orderInfo p {
  margin: 5px 0;
}

.goodsList .title {
  font-weight: bold;
  margin-bottom: 10px;
}

.goodsList ul {
  padding-left: 0;
}

.goodsList ul li {
  list-style: none;
  margin: 10px 0;
}
</style>
